#include "InputProperty.h"

InputProperty::InputProperty() {}
InputProperty::InputProperty(
    const QString& key,
    const QString& label,
    InputPropertyType type,
    bool editable
)
{
    this->_key = key;
    this->_label = label;
    this->_type = type;
    this->_editable = editable;
}

QString InputProperty::key() const
{
    return this->_key;
}

QString InputProperty::label() const
{
    return this->_label;
}

InputPropertyType InputProperty::type() const
{
    return this->_type;
}
bool InputProperty::editable() const
{
    return this->_editable;
}
QVariant& InputProperty::defaultValue()
{
    return this->_defaultValue;
}
QVariant& InputProperty::defaultValue2()
{
    return this->_defaultValue2;
}

QJsonObject* InputProperty::toJson()
{
    QJsonObject* json = new QJsonObject();
    json->insert("key", _key);
    json->insert("label", _label);

    InputProperty::setJsonPropertyType(json, "type", _type);
    InputProperty::setJsonPropertyValue(
        json, "defaultValue", this->_type, this->_defaultValue
    );
    InputProperty::setJsonPropertyValue(
        json, "defaultValue2", this->_type, this->_defaultValue2
    );
    InputProperty::setJsonPropertyValue(json, "min", this->_type, this->_min);
    InputProperty::setJsonPropertyValue(json, "max", this->_type, this->_max);
    json->insert("editable", _editable);
    return json;
}

InputProperty* InputProperty::fromJson(const QJsonValue& json)
{
    InputProperty* prop = new InputProperty();
    prop->_key = json["key"].toString();
    prop->_label = json["label"].toString();
    prop->_type = InputProperty::getJsonPropertyType(json.toObject(), "type");
    InputProperty::getJsonPropertyValue(
        json.toObject(), "defaultValue", prop->_type, prop->_defaultValue
    );
    InputProperty::getJsonPropertyValue(
        json.toObject(), "defaultValue2", prop->_type, prop->_defaultValue2
    );
    InputProperty::getJsonPropertyValue(
        json.toObject(), "min", prop->_type, prop->_min
    );
    InputProperty::getJsonPropertyValue(
        json.toObject(), "max", prop->_type, prop->_max
    );
    prop->_editable = json["editable"].toBool();
    return prop;
}

void InputProperty::setJsonPropertyValue(
    QJsonObject* json,
    const QString& key,
    const InputPropertyType type,
    const QVariant& value
)
{
    if (type == InputPropertyType::Double) {
        json->insert(key, value.toDouble());
    } else if (type == InputPropertyType::DoubleRange) {
        json->insert(key, value.toDouble());
    } else if (type == InputPropertyType::Integer) {
        json->insert(key, value.toInt());
    } else if (type == InputPropertyType::Boolean) {
        json->insert(key, value.toBool());
    } else if (type == InputPropertyType::Text) {
        json->insert(key, value.toString());
    }
}

void InputProperty::getJsonPropertyValue(
    const QJsonObject& json,
    const QString& key,
    const InputPropertyType type,
    QVariant& propValue
)
{

    if (type == InputPropertyType::Double) {
        propValue.setValue(json.value(key).toDouble());
    } else if (type == InputPropertyType::DoubleRange) {
        propValue.setValue(json.value(key).toDouble());
    } else if (type == InputPropertyType::Integer) {
        propValue.setValue(json.value(key).toInt());
    } else if (type == InputPropertyType::Boolean) {
        propValue.setValue(json.value(key).toBool());
    } else if (type == InputPropertyType::Text) {
        propValue.setValue(json.value(key).toString());
    }
}

InputPropertyType InputProperty::getJsonPropertyType(
    const QJsonObject& json,
    const QString& key
)
{
    QString value = json.value(key).toString();
    if (value == "Double") {
        return InputPropertyType::Double;
    } else if (value == "DoubleRange") {
        return InputPropertyType::DoubleRange;
    } else if (value == "Integer") {
        return InputPropertyType::Integer;
    } else if (value == "Boolean") {
        return InputPropertyType::Boolean;
    } else if (value == "Text") {
        return InputPropertyType::Text;
    }
    return InputPropertyType::Text;
}

void InputProperty::setJsonPropertyType(
    QJsonObject* json,
    const QString& key,
    const InputPropertyType value
)
{
    if (value == InputPropertyType::Double) {
        json->insert(key, "Double");
    } else if (value == InputPropertyType::DoubleRange) {
        json->insert(key, "DoubleRange");
    } else if (value == InputPropertyType::Integer) {
        json->insert(key, "Integer");
    } else if (value == InputPropertyType::Boolean) {
        json->insert(key, "Boolean");
    } else if (value == InputPropertyType::Text) {
        json->insert(key, "Text");
    }
}
